Spring Framework:4.3.14.RELEASE
本文主要是论述 Spring Framework IOC 容器中 Bean 的实例化。
Bean 的实例化主要是两种方式:
- 通过构造函数实例化
- 通过静态方法实例化
通过构造函数实例化
通过构造函数实例化,前提条件是 Bean 含有一个无参构造器,可以是隐式无参构造器,也可以是显式无参构造器。
1 | public class ExampleBeanOne { |
1 | public class ExampleBeanTwo { |
1 | <bean id="exampleBeanOne" class="com.example.spring.ExampleBeanOne"> |
通过静态方法实例化
通过静态方法实例化,前提条件是有一个静态方法可以实例化 Bean,可以是自身提供的静态方法,也可以是其他类提供的静态方法。
1 | public class ExampleBeanThree { |
1 |
|
1 | <bean id="exampleBeanThree" class="com.example.spring.ExampleBeanThree" factory-method="createInstance"> |
通过静态方法实例化不常见,推荐使用构造函数实例化
代码请参考example-03:
lxmuse-spring-example-03